<?php

namespace app\api\model;

use think\Model;

class Process extends Model
{
    protected $add_rule =   [
        'product_id'   => 'require',
        'name'   => 'require',

    ];

    protected $add_message  =   [
        'name.require' => '阶段名称不能为空',
        'product_id.require' => '所属产品不能为空',

    ];

    public function add_vali($data)
    {


        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    public function add($data)
    {
        $data['time'] = date('Y-m-d');
        $this->startTrans();
        try {
            $this->allowField(['name','type','time','user_id','status','company_id','image','content','product_id','sort','vr_url'])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'product_id'   => 'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的标识',
        'product_id.require'=>'缺失的产品',
    ];
    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
            $info = $this->find($data['id']);
            if($info['product_id'] !== $data['product_id']){
                return re_warning('已记录,非法修改他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = Process::find($data['id']);
        $info = $this->find($data['id']);
        if($info['product_id'] !== $data['product_id']){
            return re_warning('已记录,非法改变他人产品状态');
        }
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function edit($data)
    {
        $model = Process::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField(['sort','status','name','content','user_id','time','vr_url','image'])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    protected $dele_rule =   [
        'id'   => 'require',
        'product_id'=>'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的参数',
        'product_id.require'=>'缺失的产品',
    ];
    public function dele_vali($data)
    {
        try {
            validate($this->dele_rule,$this->dele_message)->check($data);
            $info = $this->find($data['id']);
            if($info['product_id'] !== $data['product_id']){
                return re_warning('已记录非法删除他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {

        $model = Process::find($id);

        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}